package net.wofly.sale.exchange.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.yunpian.sdk.util.StringUtil;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.ConditionUtil;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.condition.Order;
import net.wofly.common.condition.OrderCondition;
import net.wofly.common.json.XiNiaoJSON;
import net.wofly.framework.util.Identities;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.Brand;
import net.wofly.sale.exchange.domain.ExchangeType;
import net.wofly.sale.exchange.repository.ExchangeTypeRepository;
import net.wofly.sale.exchange.service.IExchangeTypeService;

@Service("iExchangeTypeService")
@Transactional
public class ExchangeTypeServiceImpl implements IExchangeTypeService {
	
	@Autowired
	ExchangeTypeRepository exchangeTypeRepository;

	@Override
	public ReturnBean<List<ExchangeType>> getAllExchangeType() {
		List<ExchangeType> exchangeTypes = exchangeTypeRepository.findByStatus(1) ;
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS ,"获取全部兑换类型成功！" ,exchangeTypes) ;
	}

	@Override
	public ReturnBean<ExchangeType> saveExchangeType(ExchangeType exchangeType) {
		
		if (StringUtils.isEmpty(exchangeType.getExchangeTypeID())) {
			exchangeType.setExchangeTypeID(Identities.uuid2());
			exchangeType.setStatus(1);
			exchangeType.setModifyDate(System.currentTimeMillis());
			
		}else {
			ExchangeType dbExchangeType = exchangeTypeRepository.findOne(exchangeType.getExchangeTypeID()) ;
			
			if (null == dbExchangeType) {
				return new ReturnBean<>(910113,"欲删除的的兑换类型不存在！" ) ;
			}
			
			exchangeType.setModifyDate(System.currentTimeMillis());
			exchangeType.setStatus(1);
		}
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS , "" ,exchangeTypeRepository.save(exchangeType)) ;
	}

	@Override
	public ReturnBean<String> delExchangeType(String exchangeTypeID) {
		ExchangeType dbExchangeType = exchangeTypeRepository.findOne(exchangeTypeID) ;
		if (null==dbExchangeType) {
			return new ReturnBean<>(911,"要删除的兑换类型不存在！");
		}
		
		dbExchangeType.setStatus(0);
		exchangeTypeRepository.save(dbExchangeType) ;
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS,"删除成功");
	}

	@Override
	public ReturnBean<Page<ExchangeType>> getAllExchangeType(String exchangeTypeName ,Integer pageNum, Integer pageSize) {
		Condition condition = new Condition();
        List<FilterCondition> filterConditions = new ArrayList<>();
        List<OrderCondition> orderConditions = new ArrayList<>();
        
    		filterConditions.add(new FilterCondition("1", "status", ConditionRelate.and.name(), Operation.eq.name(), 1)) ;
    		
    		if (StringUtils.isNotEmpty(exchangeTypeName)) {
    			filterConditions.add(new FilterCondition(exchangeTypeName, "exchangeTypeName", ConditionRelate.and.name(), Operation.like.name(), 2)) ;
    		}
    		
    		condition.setFilterCondition(filterConditions);
    		condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), ExchangeType.class).getResult();
    		
		Pageable pageable = new PageRequest(pageNum - 1,pageSize, new Sort(Sort.Direction.DESC, "exchangeTypeName"));
		 
		return  new ReturnBean<>(ReturnBean.CODE_SUCCESS , "" , exchangeTypeRepository.findAll(condition.getSpecification() ,pageable)) ;
	}

	@Override
	public ExchangeType findOne(String exchangeTypeID) {
		return exchangeTypeRepository.findOne(exchangeTypeID);
	}
}
